<template>
  <div class="login">
    <el-card shadow="hover" class="loginCard" style="border-radius: 25px">
      <el-form ref="loginForm" :model="loginFrom" :rules="loginRules">
        <h3 class="title">Login</h3>
        <el-form-item prop="username">
          <el-input
            v-model="loginFrom.username"
            placeholder="请输入账号"
            prefix-icon="el-icon-user"
            style="border-radius: 30px"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginFrom.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginFrom.remeberMe">记住密码</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            style="width: 100%; border-radius: 30px"
            @click="login"
          >
            <span v-if="!loading">登录</span>
            <span v-if="loading">登录中</span>
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import Cookies from "js-cookie";

export default {
  name: "Login",
  data() {
    return {
      loginFrom: {
        username: "",
        password: "",
        remeberMe: false,
      },
      loading: false,
      loginRules: {
        username: [
          { required: true, trigger: blur, message: "用户名不能为空" },
        ],
        password: [{ required: true, trigger: blur, message: "密码不能为空" }],
      },
    };
  },
  created() {
    this.getCookies();
  },
  methods: {
    getCookies() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const remeberMe = Cookies.get("remeberMe");
      this.loginFrom = {
        username: username === undefined ? this.loginFrom.username : username,
        password: password === undefined ? this.loginFrom.password : password,
        remeberMe: remeberMe === undefined ? false : Boolean(remeberMe),
      };
    },
    login() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginFrom.remeberMe) {
            Cookies.set("username", this.loginFrom.username, { expires: 7 });
            Cookies.set("password", this.loginFrom.password, { expires: 7 });
            Cookies.set("remeberMe", this.loginFrom.remeberMe, { expires: 7 });
          } else {
            Cookies.remove("username", this.loginFrom.username);
            Cookies.remove("password", this.loginFrom.password);
            Cookies.remove("remeberMe", this.loginFrom.remeberMe);
          }
          this.$store
            .dispatch("user/Login", this.loginFrom)
            .then(() => {})
            .catch(() => {
              this.loading = false;
              //this.getCode();
            });
        }
      });
    },
  },
};
</script>

<style scoped>
.login {
  /*display: flex;
  justify-content: center;
  align-items: center;*/
  /*height: 100%;*/
  background-image: url("../assets/images/login-background.png");
  /*background-size: cover;*/
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}
.loginCard {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
}
</style>
